package main

import (
	"github.com/kataras/iris/v12"
	log "github.com/sirupsen/logrus"
)

type LeaseResponse struct {
	ServerVersion         string        `json:"serverVersion"`
	ServerProtocolVersion string        `json:"serverProtocolVersion"`
	ServerGUID            string        `json:"serverGuid"`
	GroupType             string        `json:"groupType"`
	ID                    int           `json:"id"`
	LicenseType           int           `json:"licenseType"`
	EvaluationLicense     bool          `json:"evaluationLicense"`
	Signature             string        `json:"signature"`
	ServerRandomness      string        `json:"serverRandomness"`
	SeatPoolType          string        `json:"seatPoolType"`
	StatusCode            string        `json:"statusCode"`
	Offline               bool          `json:"offline"`
	ValidFrom             interface{}   `json:"validFrom"`
	ValidUntil            interface{}   `json:"validUntil"`
	Company               string        `json:"company"`
	OrderID               string        `json:"orderId"`
	ZeroIds               []interface{} `json:"zeroIds"`
	LicenseValidFrom      int64         `json:"licenseValidFrom"`
	LicenseValidUntil     int64         `json:"licenseValidUntil"`
}
type Lease1Response struct {
	ServerVersion         string      `json:"serverVersion"`
	ServerProtocolVersion string      `json:"serverProtocolVersion"`
	ServerGUID            string      `json:"serverGuid"`
	GroupType             string      `json:"groupType"`
	StatusCode            string      `json:"statusCode"`
	Msg                   interface{} `json:"msg"`
	StatusMessage         interface{} `json:"statusMessage"`
	Company               string      `json:"company"`
}

func LeaseLicense(context iris.Context) {
	clientRandomness := context.PostValue("randomness")
	username := context.PostValue("username")
	guid := context.PostValue("guid")
	offline, _ := context.PostValueBool("offline")
	log.WithFields(log.Fields{
		"platform": "jrebel",
		"username": username,
		"guid":     guid,
	}).Info("JRebel 请求许可")
	// 定义开始和结束时间
	var validFrom, validUntil interface{}
	// 并且默认设定为 空
	validFrom, validUntil = nil, nil
	if offline {
		//fmt.Println("offline")
		clientTIme, _ := context.PostValueInt64("clientTime")
		offlineDays, _ := context.PostValueInt64("offlineDays")
		offlineUntil := clientTIme + (86400 * 1000 * offlineDays)
		validFrom = clientTIme
		validUntil = offlineUntil
	}
	signature := signLicense(clientRandomness, guid, offline, validFrom, validUntil)

	// 产生 JSON
	result := LeaseResponse{
		ServerVersion:         "3.2.4",
		ServerProtocolVersion: "1.1",
		ServerGUID:            serverGUID,
		GroupType:             "managed",
		ID:                    1,
		LicenseType:           1,
		EvaluationLicense:     false,
		Signature:             signature,
		ServerRandomness:      serverRandomness,
		SeatPoolType:          "standalone",
		StatusCode:            "SUCCESS",
		Offline:               offline,
		ValidFrom:             validFrom,
		ValidUntil:            validUntil,
		Company:               username,
		OrderID:               "",
		ZeroIds:               make([]interface{}, 0),
		LicenseValidFrom:      1490544001000,
		LicenseValidUntil:     1691839999000,
	}
	context.JSON(result)

}
func Lease1License(context iris.Context) {
	username := context.URLParam("username")
	log.WithFields(log.Fields{
		"platform": "jrebel",
		"username": username,
	}).Info("JRebel 请求许可1")
	result := Lease1Response{
		ServerVersion:         "3.2.4",
		ServerProtocolVersion: "1.1",
		ServerGUID:            serverGUID,
		GroupType:             "managed",
		StatusCode:            "SUCCESS",
		Msg:                   nil,
		StatusMessage:         nil,
		Company:               username,
	}
	context.JSON(result)

}
